import {
  getContactorList,
  deleteContactor
} from '../../../services/api/menber'
import Toast from 'tdesign-miniprogram/toast/index';
// const initFilters = {
//   overall: 1,
//   sorts: '',
//   layout: 0,
// };
Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    goodsListLoadStatus: 0,
    pageLoading: false,
    isActive: null,
    layout: 0,
    sorts: '',
    overall: 1,
    show: false,
    minVal: '',
    maxVal: '',
    hasLoaded: false,
    loadMoreStatus: 0,
    loading: true,
    listTitles: [],
    fixedTitle: null,
    toView: 'inToView0',
    oHeight: [],
    scroolHeight: 0,
    userList: [],
    agentUserId: '',
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    index: 0,
    num: 20,
  },
  showFilterPopup() {
    this.setData({
      show: true,
    });
  },
  add() {
    wx.navigateTo({
      url: `/subpages/pages/addContactor/addContactor?agentId=${this.data.agentUserId}`,
    })
  },
  onLoad: function (options) {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查
    let agentUserId = options.agentUserId;
    this.setData({
      agentUserId
    })
    that.getList();
  },
  toDetail(e) {
    wx.navigateTo({
      url: `/subpages/pages/addContactor/addContactor?isEdit=true&kid=${e.currentTarget.dataset.group.kid}&agentId=${e.currentTarget.dataset.group.agentUserId}`,
    })
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },

  async toDelete(e) {
    let {
      data
    } = await deleteContactor({
      kid: e.currentTarget.dataset.group.kid,
      opType: 1
    });
    let index = this.data.userList.findIndex((el) => {
      return el.kid == e.currentTarget.dataset.group.kid
    });
    this.data.userList.splice(index, 1);
    this.setData({
      userList: this.data.userList
    })
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.num;
    let pageIndex = this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }
    try {
      const {
        data
      } = await getContactorList({
        agentUserId: this.data.agentUserId,
        pageSize,
        pageNo: pageIndex,
      });
      console.log(data, '!!!!!!');
      this.setData({
        userList: fresh ? data : this.data.userList.concat(data),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },


  async getList(id) {
    let {
      data
    } = await getContactorList({
      agentUserId: this.data.agentUserId
    });
    this.setData({
      userList: data
    })
  },
})